package com.tj.seed.common.codegenerator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.FileType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import java.util.ArrayList;
import java.util.List;

public class MyBatisPlusCodeGenerator {
    private static String DRIVER_NAME="com.mysql.jdbc.Driver";
    private static String DATA_SOURCE_URL="jdbc:mysql://114.132.222.143:3306/create_data?useUnicode=true&useSSL=false&characterEncoding=utf8";
    private static String USER_NAME="root";
    private static String PASSWORD="zhitong520!";
    private static String PROJECT_MODULE_NAME="admin";
    private static String MODULE_NAME="admin";
    private static String TABLE_INCLUDE="admin_user_menu";//多个表用,号隔开
    private static String AUTHOR="tz";
    private static boolean IS_UPDATE=true;//是否对已有文件进行覆盖
    public static void main(String[] args) {
        AutoGenerator generator=new AutoGenerator();
        //全局配置
        GlobalConfig config=new GlobalConfig();
        String projectPath=System.getProperty("user.dir");
        config.setOutputDir(projectPath+"/src/main/java");
        config.setAuthor(AUTHOR);
        config.setOpen(false);
        generator.setGlobalConfig(config);
        //数据源配置
        DataSourceConfig dsc=new DataSourceConfig();
        dsc.setUrl(DATA_SOURCE_URL);
        dsc.setDriverName(DRIVER_NAME);
        dsc.setUsername(USER_NAME);
        dsc.setPassword(PASSWORD);
        generator.setDataSource(dsc);
        //包配置
        PackageConfig packageConfig=new PackageConfig();
        packageConfig.setModuleName(MODULE_NAME);
        packageConfig.setParent("com.test.datacreator");
        generator.setPackageInfo(packageConfig);
        //自定义配置
        InjectionConfig icg=new InjectionConfig() {
            @Override
            public void initMap() {
                // todo
            }
        };
        icg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                return IS_UPDATE;
            }
        });
        //模板引擎
        String templatePath="/templates/mapper.xml.vm";
        //自定义输出配置
        List<FileOutConfig> focList=new ArrayList<>();
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath+"/src/main/resources/mapper/"+packageConfig.getModuleName()
                        +"/"+tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
            }
        });
        icg.setFileOutConfigList(focList);
        generator.setCfg(icg);
        //配置模板
        TemplateConfig templateConfig=new TemplateConfig();
        templateConfig.setXml(null);
        generator.setTemplate(templateConfig);
        //策略配置
        StrategyConfig strategyConfig=new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        //strategyConfig.setSuperEntityClass();
        //strategyConfig.setSuperEntityColumns("id");//父类中的公共字段
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        //strategyConfig.setSuperControllerClass();
        strategyConfig.setInclude(TABLE_INCLUDE);
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setTablePrefix(packageConfig.getModuleName()+"_");
        generator.setStrategy(strategyConfig);
        generator.setTemplateEngine(new VelocityTemplateEngine());
        generator.execute();



    }
}
